Traversing Java Object Arrays [migrated]

Posted by Sundi on Programmers See other posts from Programmers or by Sundi
Published on 2011-11-23T14:39:11Z Indexed on 2011/11/23 18:04 UTC
Read the original article Hit count: 148

Filed under:

Please Help. Program does not read Array rentBooks[] in the for() loop this option is selected when choosing option 2 then option 4 in the menu The Array reads perfectly when reading the items after the setBook() Method

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.*;
import java.util.Locale;
import java.text.SimpleDateFormat ;

class Library
{
    protected static String Author;
    protected static String Title;
    SimpleDateFormat PublicationDate;
    int itemCode;
    int available = 1;
}

class Book extends Library 
{
    protected static String PublisherName;
    protected static String Edition;
    static Book[] rentBooks = new Book[5];

    //Book[] rentBooks = new Book[5];
    int count = 0;  

        public Book()
        {
             String start= "start";
            showBook.main(anza);
        }

        public void setBook( String Auth, String Titl, String PublishName)
        {
            this.Author = Auth;
            this.Title = Titl;
            this.PublisherName = PublishName;       
        }


        public void getBook()
        {

          //System.out.println("*************BOOKS*************************");
          System.out.println( "\n\nThe Author of the first Book is "+ this.Author );
          System.out.println( "The Title of the book is "+ this.Title);
          System.out.println( "The Publisher of the book is "+ this.PublisherName );
         // System.out.println( "The Edition of the book is "+ Edition );

        } 
}


class showBook{

    static Book[] rentBooks = new Book[5];
    static Book[] rentBooks2 = new Book[5];

    static int a,b;

    //for ( a=0; a < 5;  a++ )
                //rentBooks2[a] = new Book();

  public static void main(String[] args)
  {

        File file = new File("Book2.txt");

        //Book libraryBooks = new Book();

        int j;

        //initialise Array Class Objects
         for( j = 0; j < 5; j++) {
                rentBooks[j] = new Book();   }

        int i = 0;


        try{    
                Scanner scanner = new Scanner(file);
                scanner.useDelimiter(",");
                String loan="";
                int loan2;

                while( scanner.hasNextLine())
                {  //Should the Books be Stored in An Array?
                   // At the moment you have separate objects stored in unknown location    


                    String Author = scanner.next();
                    String Title = scanner.next();
                    String PublisherName = scanner.next();


                    if ( i < 4)
                    {
                    System.out.println(i);
                    rentBooks[i].setBook(Author, Title, PublisherName);
                   rentBooks[i].getBook(); // MEMBERS SHOWN
                    i++;


                    }



public class readBook4{

  public static void main(String[] args)
  {

        int number =0;

        System.out.println( "Please select one of the choices below " );
        System.out.println( "Select option 1 to list all items in the library ");
        System.out.println( "Select option 2 to list the items by category");
        System.out.println( "Select option 3 to choose item available in the library ");
        System.out.println( "Select option 7 to exit " );

       InputStreamReader isr = new InputStreamReader( System.in);

       BufferedReader buffer = new BufferedReader( isr);
       String input = "";


       try
       {
            input = buffer.readLine();

            number = Integer.parseInt(input);
        //int number = Integer.parseInt( Edition);

        if ( number == 1 )
        {
           System.out.println( " \nThanks you are reading "+ input);    
           //showStudent.main(args);
           showPeriodical.main(args);
           showDVD.main(args);
          // showBook.main(args);
        }

        if ( number == 2 )
        {       
            //jht.cls();
            int number2;

            System.out.println( "Please select one of the choices below " );
            System.out.println( "Select option 4 to list Books only ");
            System.out.println( "Select option 5 to list the Periodicals only");
            System.out.println( "Select option 6 to list DVDs only");

            InputStreamReader isr2 = new InputStreamReader(System.in);
            BufferedReader buffer2 = new BufferedReader(isr2);
            String input2 = "";

                    try
                    {
                       input2 = buffer2.readLine();
                        buffer.close();
                    }
                    catch(IOException e)
                    {
                        System.out.println("An input error has occured");
                    }   

                    //System.out.println("Thanks, you are reading" + input2);

                    number2 = Integer.parseInt(input2);

                    if ( number2 == 4 )
                    {
                        showBook.main(args);
                    }

                    if ( number2 == 5 )
                    {
                        showPeriodical.main(args);

                    }   

                    if ( number2 == 6 )
                    {
                        showDVD.main(args);

                    }   



                //  readBook4.main(args);



        }

        if( number == 3 )
        {
            //showBook.main(args);
            showBook.availableBooks();
            showDVD.availableDVD();
            showPeriodical.availablePeriodical();

        }

        if ( number == 7 )
        {
            showStudent.main(args);

        }   





            buffer.close();

       }
       catch( IOException e )
       {
          System.out.println( " An input error has occured ");

        }

        //System.out.println( " \nThanks you are reading "+ input); 



  }

}   

            }
                //buffer.close();

                scanner.close();

            }

            catch( FileNotFoundException e)
            {
              System.out.println("File not Found");

            }
            for ( i=0; i < 5;  i++ )
                rentBooks[i].getBook(); //ARRAY NOT SHOWN

  } 

}  

© Programmers or respective owner

Related posts about java